<template>
  <div class="search_container">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 8px">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>借阅信息</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="always">
      <!-- 搜索内容和导出区域 -->
      <el-row style="height: 45px">
        <el-col :span="1.8">
          <span style="line-height: 40px">条件搜索:</span>
        </el-col>
        <el-col :span="5">
          <el-select v-model="queryInfo.query" filterable placeholder="请选择" size="large">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="4">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.condition"
            class="input-with-select"
            @keyup.enter.native="searchBookBorrowByPage"
            size="large"
          ></el-input>
        </el-col>
        <el-col :span="2">
          <el-button
            type="primary"
            class="el-icon-printer"
            size="large"
            @click="searchBookBorrowByPage"
          >
            <el-icon><Search /></el-icon>
          </el-button>
        </el-col>

        <el-col :span="3">
          <el-button type="primary" class="el-icon-printer" size="large" @click="exportToExcel">
            导出当前页
          </el-button>
          <!-- </vue-json-excel> -->
        </el-col>
        <el-col :span="3">
          <el-button size="large" type="success" @click="AllExport = true">导出所有信息</el-button>
          <el-dialog v-model="AllExport" title="Tips" width="500">
            <span>确定要导出所有信息吗？</span>
            <br />
            <br />
            <span style="color: red">注：导出时间较长，请耐心等待</span>
            <template #footer>
              <div class="dialog-footer">
                <el-button @click="AllExport = false">取消</el-button>
                <el-button type="primary" @click="exportToAll">确认导出</el-button>
              </div>
            </template>
          </el-dialog>
        </el-col>

        <el-col :span="1">
          <el-button type="success" class="el-icon-full-screen" size="large" @click="fullScreen">
            全屏
          </el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table
        height="300"
        :data="tableData"
        style="width: 100%"
        id="pdfDom"
        :default-sort="{ prop: 'cardNumber', order: 'ascending' }"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column prop="cardNumber" label="借阅编号" sortable></el-table-column>
        <el-table-column prop="bookNumber" label="图书编号" sortable></el-table-column>
        <el-table-column prop="borrowDate" label="借阅日期" sortable></el-table-column>
        <el-table-column prop="closeDate" label="截止日期" sortable></el-table-column>
        <el-table-column prop="returnDate" label="归还日期" sortable></el-table-column>
      </el-table>
      <!-- 分页查询区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 3, 4, 5]"
        :page-size="queryInfo.pageSize"
        layout="total,  prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script setup>
import * as XLSX from "xlsx";
import { reactive, ref, onMounted } from "vue";
import { getBookBorrows, ExportAllBook } from "../../api/api";
const options = reactive([
  {
    value: "book_number",
    label: "图书编号",
  },
  {
    value: "borrow_date",
    label: "借阅日期",
  },
  {
    value: "close_date",
    label: "截止日期",
  },
  {
    value: "return_date",
    label: "归还日期",
  },
]);
const tableData = ref([]);
//全部导出框的显示与隐藏
let AllExport = ref(false);
const queryInfo = reactive({
  pageNum: 1,
  pageSize: 5,
  condition: "",
  query: "",
});
let cardNumber = window.sessionStorage.getItem("cardNumber");
const total = ref(0);
let title = "图书借阅表格";
let json_fields = {
  借阅编号: "cardNumber",
  图书编号: "bookNumber",
  借阅日期: "borrowDate",
  截止日期: "closeDate",
  归还日期: "returnDate",
};
let loading = ref(true);

onMounted(() => {
  searchBookBorrowByPage();
});

//导出所有图书
const exportToAll = async () => {
  let data = { condition: "", query: "" };
  (data.condition = queryInfo.condition), (data.query = queryInfo.query);
  AllExport.value = false;
  await ExportAllBook(data);
};

//导出excel
const exportToExcel = () => {
  //excel表格头部
  const headers = ["借阅编号", "图书编号", "借阅日期", "截止日期", "归还日期"];
  // 转换表格数据为数组
  const ExcleData = tableData.value.map(item => [
    item.cardNumber,
    item.bookNumber,
    item.borrowDate,
    item.closeDate,
    item.returnDate,
  ]);
  // 创建一个工作簿
  const workbook = XLSX.utils.book_new();
  // 创建一个工作表
  const worksheet = XLSX.utils.aoa_to_sheet([headers, ...ExcleData]);

  // 将工作表添加到工作簿
  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
  // 生成Excel文件
  XLSX.writeFile(workbook, "图书借阅信息表格.xls");
};

const handleSizeChange = val => {
  queryInfo.pageSize = val;
  searchBookBorrowByPage();
};

const handleCurrentChange = val => {
  queryInfo.pageNum = val;
  searchBookBorrowByPage();
};
//查询信息
const searchBookBorrowByPage = () => {
  getBookBorrows(queryInfo)
    .then(res => {
      tableData.value = res.data.records;
      total.value = res.data.total;
      ElMessage({
        message: "获取信息成功",
        type: "success",
      });
    })
    .catch(err => {
      console.log(err);
      if (err.response.data.message == "Borrow Not Found") {
        ElMessage.error("暂无借阅信息");
      } else {
        ElMessage.error("获取信息失败");
      }
    });
  loading.value = false;
};

const downLoad = function () {
  getPdf(title); //参数是下载的pdf文件名
};
//全屏
const fullScreen = function () {
  // Dom对象的一个属性: 可以用来判断当前是否为全屏模式(trueORfalse)
  let full = document.fullscreenElement;
  // 切换为全屏模式
  if (!full) {
    // 文档根节点的方法requestFullscreen实现全屏模式
    document.documentElement.requestFullscreen();
  } else {
    // 退出全屏模式
    document.exitFullscreen();
  }
};
</script>

<style></style>
